package com.itheima.case2.web;

import com.itheima.case2.pojo.po.Role;
import com.itheima.case2.pojo.vo.Result;
import com.itheima.case2.service.RoleService;
import com.itheima.case2.utils.BaseController;
import com.itheima.case2.utils.BeansFactory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/role/*")
public class RoleServlet extends BaseServlet{
    public void addRole(HttpServletRequest request, HttpServletResponse response){
        System.out.println("增加角色");
    }
    public void deleteRole(HttpServletRequest request, HttpServletResponse response){
        System.out.println("增加角色");
    }
    public void updateRole(HttpServletRequest request, HttpServletResponse response){
        System.out.println("增加角色");
    }
    public void queryRole(HttpServletRequest request, HttpServletResponse response){
        try {
            //1.创建实现类对象
            RoleService roleService = BeansFactory.getBean("roleService");
            //2.调用实现类中的查询角色的方法
            List<Role> roleList= roleService.queryAllRole();
            //3.将查询结果返回
            //3.1查询成功
            Result result = new Result(true, "查询角色成功", roleList);
            BaseController.printResult(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            //3.2查询失败b
            Result result = new Result(false, "查询角色失败");
            try {
                BaseController.printResult(response,result);
            } catch (Exception ioException) {
                ioException.printStackTrace();
            }
        }
    }

}
